package net.dyxy.yinyy.jxzljk.student1;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.SimpleAdapter;
import android.widget.AdapterView.OnItemClickListener;
import android.os.Build;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_main, container,
					false);
	        GridView gridview = (GridView)rootView.findViewById(R.id.grid01);
	        ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();
	        String[] text = new String[]{
	        	"请假销假","课程表","违纪预警","信息推送"
	        };
	        int[] image = new int[]{
	        		 R.drawable.course, R.drawable.leave,
	        		 R.drawable.truang, R.drawable.message
	        };
	        for(int i = 0;i < 9;i++)
	        {
	            HashMap<String, Object> map = new HashMap<String, Object>();
	            map.put("ItemImage", image[i%4]);
	            map.put("ItemText", text[i%4]);
	            meumList.add(map);
	        }
	        SimpleAdapter saItem = new SimpleAdapter(getActivity(),
	                  meumList, //数据源
	                  R.layout.gridgew, //xml实现
	                  new String[]{"ItemImage","ItemText"}, //对应map的Key
	                  new int[]{R.id.Image,R.id.Text});  //对应R的Id
	  
	                //添加Item到网格中
	                gridview.setAdapter(saItem);
	                //添加点击事件
	                gridview.setOnItemClickListener(
	                    new OnItemClickListener()
	                    {
	                        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3)
	                        {
	                            int index=arg2%4;
	            				switch (index) {
	            				case 0:
	            					Intent intent = new Intent(getActivity(),LeaveListActivity.class);
	            					startActivity(intent);
//	            					qingjiaFragment fragment1 = new qingjiaFragment();
//	            					getFragmentManager().beginTransaction()
//	            							.replace(R.id.container, fragment1).commit();
	            					break;
	            				case 1:
	            					Intent intent1 = new Intent(getActivity(),CourseScheduleActivity.class);
	            					startActivity(intent1);
//	            					kechengFragment fragment2 = new kechengFragment();
//	            					getFragmentManager().beginTransaction()
//	            							.replace(R.id.container, fragment2).commit();
	            					break;
	            				case 2:
	            					Intent intent2 = new Intent(getActivity(),TruancyListActivity.class);
	            					startActivity(intent2);
	            					break;
	            				case 3:
	            					Intent intent3 = new Intent(getActivity(),MessageListActivity.class);
	            					startActivity(intent3);
	            					break;
	            				default:
	            					break;
	            				}
	                            //Toast用于向用户显示一些帮助/提示
	                        }
	                    }
	                );
					return rootView;

//			qingjia = (ImageButton) rootView.findViewById(R.id.imageButton11);
//			kecheng = (ImageButton) rootView.findViewById(R.id.imageButton12);
//
//			qingjia.setOnClickListener(listener0);
//			kecheng.setOnClickListener(listener0);
//
//			return rootView;
//		}
//
//		OnClickListener listener0 = new OnClickListener() {
//			public void onClick(View v) {
//
//				int buttonid = v.getId();
//				switch (buttonid) {
//				case R.id.imageButton11:
//					qingjiaFragment fragment1 = new qingjiaFragment();
//					getFragmentManager().beginTransaction()
//							.replace(R.id.container, fragment1).commit();
//					break;
//				case R.id.imageButton12:
//					kechengFragment fragment2 = new kechengFragment();
//					getFragmentManager().beginTransaction()
//							.replace(R.id.container, fragment2).commit();
//					break;
//				default:
//					break;
//				}
//			}
//		};
	}
	}

}
